<?php
namespace api\controllers;

use Yii;
use yii\rest\ActiveController;
use yii\data\ActiveDataProvider; 
use api\models\Ret;
use yii\helpers\ArrayHelper;
use yii\filters\auth\QueryParamAuth;
use common\models\Account;
use common\models\Article;

class ArticleController extends ActiveController
{ 
    public $modelClass = 'common\models\Article';
    
    public function behaviors(){
        return ArrayHelper::merge(parent::behaviors(), [
            'authenticatior' => [
                'class' => QueryParamAuth::className()
            ]
        ]); 
    }
    public function actions(){
        $action= parent::actions(); // TODO: Change the autogenerated stub
        unset($action['index']);
        // unset($action['create']);
        // unset($action['update']);
        // unset($action['delete']);
    }

    public function actionIndex(){
        $ssid = Yii::$app->request->get("ssid");
        if(!$ssid){
            // return new Ret(-1,'缺少SSID参数',[]);
            // 如果没有参数，则获取用户下的第一条明星数据
            $account = new Account();
            
            $ssid = $account->getSurperByToken(Yii::$app->request->get("access-token"));
        }
        
        $modelClass = $this->modelClass;
        $query = $modelClass::find()->where('ss_id='.$ssid);
        $res = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => 10,
            ],
            'sort'  => [                        // sort 用于排序
                'defaultOrder' => [
                    'atcl_negative_prob' => SORT_DESC,          // defaultOrder 指定默认排序字段
                ],
                'attributes' => ['atcl_negative_prob'], // attribute 指定那几个字段可以用来排序
            ],
        ]);
        return  new Ret(1,'获取成功',$res->getModels());
        
    }
    public function actionContent(){
        $id = Yii::$app->request->get("id");
        $modelClass = $this->modelClass;
        $query = $modelClass::find()->where('atcl_id='.$id);
        $res = new ActiveDataProvider([
            'query' => $query,
        ]);
        return  new Ret(1,'获取成功',$res->getModels());
    }
    public function actionHandle(){
        $id= Yii::$app->request->get("id");
        $atcl = new Article();
        if($atcl->subHandle($id)){
            return  new Ret(1,'提交成功', []);
        }else{
            return  new Ret(-1,'提交失败', []);
        }
        
    }
}
